﻿namespace HuwmanCode.UserAccountControl

open System
open System.Diagnostics
open System.Runtime.InteropServices

[<AutoOpen>]
module internal NativeMethods =
    type TokenDesiredAccess =
    | StandardRightsRequired = 0x000F0000
    | AssignPrimary = 0x0001
    | Duplicate = 0x0002
    | Impersonate = 0x0004
    | Query = 0x0008
    | QuerySource = 0x0010
    | AdjustPrivileges = 0x0020
    | AdjustGroups = 0x0040
    | AdjustDefault = 0x0080
    | AdjustSessionId = 0x0100

    type TokenInformationType =
    | TokenUser = 1
    | TokenGroups = 2
    | TokenPrivileges = 3
    | TokenOwner = 4
    | TokenPrimaryGroup = 5
    | TokenDefaultDacl = 6
    | TokenSource = 7
    | TokenType = 8
    | TokenImpersonationLevel = 9
    | TokenStatistics = 10
    | TokenRestrictedSids = 11
    | TokenSessionId = 12
    | TokenGroupsAndPrivileges = 13
    | TokenSessionReference = 14
    | TokenSandBoxInert = 15
    | TokenAuditPolicy = 16
    | TokenOrigin = 17
    | TokenElevationType = 18
    | TokenLinkedToken = 19
    | TokenElevation = 20
    | TokenHasRestrictions = 21
    | TokenAccessInformation = 22
    | TokenVirtualizationAllowed = 23
    | TokenVirtualizationEnabled = 24
    | TokenIntegrityLevel = 25
    | TokenUIAccess = 26
    | TokenMandatoryPolicy = 27
    | TokenLogonSid = 28
    | MaxTokenInfoClass = 29

    [<DllImport("advapi32.dll", SetLastError = true)>]
    extern bool OpenProcessToken(nativeint processHandle, TokenDesiredAccess desiredAccess, 
        [<Out>] nativeint& tokenHandle);

    [<DllImport("advapi32.dll", SetLastError = true)>]
    extern bool GetTokenInformation(nativeint tokenHandle, TokenInformationType tokenInformationType,
        nativeint tokenInformation, int tokenInformationLength, [<Out>] int& returnLength);

    [<DllImport("kernel32.dll", SetLastError = true)>]
    extern bool CloseHandle(nativeint handle);

   